/*
 * JGO2RDF creates a RDF representation of GAF2 files, which are automatically sourced from GO ftp site.
 * Copyright (C) 2012 Matthew M. Hindle & Artem Lysenko
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package uk.co.jgo2rdf.goa.deligates;

import com.google.common.io.Files;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDFS;
import org.apache.log4j.Logger;
import uk.co.jgo2rdf.exception.AccessionNonConformentException;
import uk.co.jgo2rdf.goa.gp2protein.EntityType;
import uk.co.jgo2rdf.goa.gp2protein.ParseGP2XRefFile;
import uk.co.jgo2rdf.rdf.lsrn.LSRNVocab;
import uk.co.jgo2rdf.rdf.refseq.URIs;
import uk.co.jgo2rdf.rdf.sio.SIOVocab;
import uk.co.jgo2rdf.uniprotidmapping.UniprotIdMappingIndex;

import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: mhindle
 * Date: 14/02/12
 * Time: 16:05
 * To change this template use File | Settings | File Templates.
 */
public class TAIRRDFizer implements EntityRDFizer {

    private static final Logger log = Logger.getLogger(TAIRRDFizer.class);

    private static Pattern regexCheck = Pattern.compile("AT(\\d+|[M|C])[G]\\d+(\\.\\d+){0,1}");

    public static boolean isAGILocusId(String accession) {
        String accessionComp = accession.trim().toUpperCase();
        if (accessionComp.length() > 2 && accessionComp.startsWith("AT")) { //pre-filtering for performance
            return regexCheck.matcher(accessionComp).matches();
        }
        return false;
    }

    @Override
    public Resource rdfize(Model jenaModel, String accession) throws Exception {
        accession = preProcessAccession(accession);
        if (accession.startsWith("GENE:")) {     //GENE means transcript here
            Resource record = jenaModel.createResource(URIs.TAIR_ACCESSION + accession, LSRNVocab.TAIR_Record);
            Resource identifier = jenaModel.createResource(URIs.TAIR_ACCESSION + accession + "-id", LSRNVocab.TAIR_Identifier);

            record.addProperty(SIOVocab.has_attribute, identifier);
            record.addLiteral(RDFS.label, accession);

            identifier.addLiteral(SIOVocab.has_value, accession);
            identifier.addLiteral(RDFS.label, accession + "-id");

            Resource rna = jenaModel.createResource(URIs.TAIR_ACCESSION + accession + "-rna", SIOVocab.rna);
            rna.addLiteral(RDFS.label, accession + "-rna");
            record.addProperty(SIOVocab.is_about, rna);
            return rna;
        } else if (accession.startsWith("LOCUS:")) {
            Resource record = jenaModel.createResource(URIs.TAIR_ACCESSION + accession, LSRNVocab.TAIR_Record);
            Resource identifier = jenaModel.createResource(URIs.TAIR_ACCESSION + accession + "-id", LSRNVocab.TAIR_Identifier);

            identifier.addLiteral(SIOVocab.has_value, accession);
            identifier.addLiteral(RDFS.label, accession + "-id");

            Resource dna = jenaModel.createResource(URIs.TAIR_ACCESSION + accession + "-dna", SIOVocab.dna);
            dna.addLiteral(RDFS.label, accession + "-dna");
            record.addProperty(SIOVocab.is_about, dna);

            Resource rna = jenaModel.createResource(URIs.TAIR_ACCESSION + accession + "-rna", SIOVocab.rna);
            rna.addLiteral(RDFS.label, accession + "-rna");
            dna.addProperty(SIOVocab.is_transcribed_into, rna);
            return dna;
        }
        log.error("Could not rdfize " + accession);
        throw new AccessionNonConformentException("Could not rdfize " + accession);
    }

    @Override
    public EntityType getType(String accession) throws Exception {
        if (accession.startsWith("GENE:")) {
            return EntityType.RNA;
        } else if (accession.startsWith("LOCUS:")) {
            return EntityType.DNA;
        }
        return EntityType.DNA;
    }

    @Override
    public String preProcessAccession(String accession) {
        return accession.trim().toUpperCase();
    }
}
